package com.hot12345.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hot12345.database.BaseDaoImpl;
import com.hot12345.entity.Click;
import com.hot12345.server.DBConnection.IbatisConnectCommImpl;
import com.ibatis.sqlmap.client.SqlMapClient;

public class ClickDaoImpl extends BaseDaoImpl<Click> {

	private final static Log log = LogFactory.getLog(InfoDaoImpl.class);
	private final static ClickDaoImpl instance = new ClickDaoImpl();
	public final static String RDS = "defaultRDs";
	public final static String WDS = "defaultWDs";
	
	private ClickDaoImpl(){}
	
	public static ClickDaoImpl getInstance(){
		return instance;
	}
	
	public int addNum(long infoID) throws Exception{
		String updateSqlMapId = getEntityName() + ".addNum";
		log.debug("updateSqlMapId=" + updateSqlMapId);
		SqlMapClient sqlClient = IbatisConnectCommImpl.getSqlMapClient(WDS);
		int effectCnt = 0;
		try {
			effectCnt = sqlClient.update(updateSqlMapId, infoID);
			if(effectCnt==0){
				log.error("update failure");
				throw new Exception("jdbc.error.update");  
			}
		} catch (SQLException e) {
			throw new Exception("jdbc.error.update");  
		}
		return effectCnt;
	}
	
	/**
	 * 按日期查询点击排行
	 *	
	 * @param params
	 * @param pageNo
	 * @param limit
	 * @return
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-3-14 上午12:26:11
	 * @version V1.0
	 */
	public List<Click> findByTop(Map<String, Object> params,
			int pageNo, int limit) throws Exception{
		if(params==null){
			params=new HashMap<String, Object>();
		}
		params.put("startNo", pageNo);
		params.put("pageSize", limit);
		String findSqlMapId = getEntityName() + ".findByTop";
		log.debug("findSqlMapId=" + findSqlMapId);
		SqlMapClient sqlClient = IbatisConnectCommImpl.getSqlMapClient(RDS);
		try {
			return (List<Click>) sqlClient.queryForList(
					findSqlMapId, params);
		} catch (SQLException e) {
			log.error("SQLException " ,e);
			throw new Exception("jdbc.error.findByParamsPage"+e);
		}
	}
	
	public int updateItem(Long infoID,Long parentID, int itemID) throws Exception{
		Click click = new Click();
		click.setInfoID(infoID);
		click.setParentID(parentID);
		click.setItemID(itemID);
		String updateSqlMapId = getEntityName() + ".updateItem";
		log.debug("updateSqlMapId=" + updateSqlMapId);
		SqlMapClient sqlClient = IbatisConnectCommImpl.getSqlMapClient(WDS);
		int effectCnt = 0;
		try {
			effectCnt = sqlClient.update(updateSqlMapId, click);
			if(effectCnt==0){
				log.info("更新失败，没有更新成功任何数据。");
			}
		} catch (SQLException e) {
			log.error("SQLException " ,e);
			throw new Exception("jdbc.error.update");  
		}
		return effectCnt;
	}
	
	/**
	 * 冻结
	 *	
	 * @param infoID
	 * @return
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-5-10 下午04:37:50
	 * @version V1.0
	 */
	public int freeze(long infoID) throws Exception{
		return this.updateState(infoID,0);
	}
	
	/**
	 * 恢复正常状态 
	 *	
	 * @param infoID
	 * @return
	 * @author wuzongbao
	 * @date 2013-5-10 下午04:38:03
	 * @version V1.0
	 * @throws Exception 
	 */
	
	public int restore(long infoID) throws Exception{
		return this.updateState(infoID,1);
	}
	
	private int updateState(Long infoID, int state) throws Exception{
		Click click = new Click();
		click.setInfoID(infoID);
		click.setState(state);
		String updateSqlMapId = getEntityName() + ".updateState";
		log.debug("updateSqlMapId=" + updateSqlMapId);
		SqlMapClient sqlClient = IbatisConnectCommImpl.getSqlMapClient(WDS);
		int effectCnt = 0;
		try {
			effectCnt = sqlClient.update(updateSqlMapId, click);
			if(effectCnt==0){
				log.info("更新失败，没有更新成功任何数据。");
			}
		} catch (SQLException e) {
			log.error("SQLException " ,e);
			throw new Exception("jdbc.error.update");  
		}
		return effectCnt;
	}
}
